#ifndef _NTH_LIB_H
#include <nth-lib.h>
#endif

#ifndef _STDIO_H
#include <stdio.h>
#endif

#ifndef _STRING_H
#include <string.h>
#endif

int Clean_Nth_Plugin(
	struct Nth_Plugin * nth_plugin
){
	if (!nth_plugin){
		return NTH_ERROR_NULL_POINTER;
	}

	if (nth_plugin->plugin_name){
		free(nth_plugin->plugin_name);
		nth_plugin->plugin_name = 0;
	}

	if (nth_plugin->plugin_description){
		free(nth_plugin->plugin_description);
		nth_plugin->plugin_description = 0;
	}

	return 0;
}

int Initialize_Nth_Plugin(
	struct Nth_Plugin * nth_plugin
){
	char plugin_name[] = "version";
	char plugin_desc[] = "Get current version of nth";

	if (!nth_plugin){
		return NTH_ERROR_NULL_POINTER;
	}

	nth_plugin->plugin_name = malloc(sizeof(plugin_name));

	if (!nth_plugin->plugin_name){
		return NTH_ERROR_NO_MEMORY;
	}

	memcpy(
		nth_plugin->plugin_name,
		plugin_name,
		sizeof(plugin_name)
	);

	nth_plugin->plugin_description = malloc(sizeof(plugin_desc));

	if (!nth_plugin->plugin_description){
		free(nth_plugin->plugin_name);
		nth_plugin->plugin_name = 0;
		return NTH_ERROR_NO_MEMORY;
	}

	memcpy(
		nth_plugin->plugin_description,
		plugin_desc,
		sizeof(plugin_desc)
	);

	nth_plugin->plugin_callback = Nth_Plugin_Callback;

	return 0;
}

int Nth_Plugin_Callback(
	struct Nth_Program * nth_program,
	struct Nth_Number * plugin_number
){
	int error = 0;

	char * version_type = 0;

	const unsigned int version_major = 0;
	const unsigned int version_minor = 1;
	const unsigned int version_patch = 0;

	size_t major_diff = 0;
	size_t minor_diff = 0;
	size_t patch_diff = 0;

	if (!nth_program){
		return 1;
	}

	error = Get_Next_Nth_Argument(
		nth_program,
		&version_type
	);

	if (error){

		Push_Nth_Failure(
			nth_program,
			"Nth_Plugin_Callback",
			"Unrecognized version type"
		);

		return 1;
	}

	major_diff = strcmp(version_type, "major");

	if (major_diff == 0){
		fprintf(stdout, "%u\n", version_major);
		return 0;
	}

	minor_diff = strcmp(version_type, "minor");

	if (minor_diff == 0){
		fprintf(stdout, "%u\n", version_minor);
		return 0;
	}

	patch_diff = strcmp(version_type, "patch");

	if (patch_diff == 0){
		fprintf(stdout, "%u\n", version_patch);
		return 0;
	}

	return 0;
}

